<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 该类是后台与前台都需要继承的核心类
 * 包含前后台公用的方法
 */
class CIC_Controller extends CI_Controller
{
	/** CI代码提示 start */
	/**
	 * @var CI_Loader
	 */
	var $load;
	/**
	 * @var CI_DB_active_record
	 */
	var $db;
	/**
	 * @var CI_Calendar
	 */
	var $calendar;
	/**
	 * @var Email
	 */
	var $email;
	/**
	 * @var CI_Encrypt
	 */
	var $encrypt;
	/**
	 * @var CI_Ftp
	 */
	var $ftp;
	/**
	 * @var CI_Hooks
	 */
	var $hooks;
	/**
	 * @var CI_Image_lib
	 */
	var $image_lib;
	/**
	 * @var CI_Language
	 */
	var $language;
	/**
	 * @var CI_Log
	 */
	var $log;
	/**
	 * @var CI_Output
	 */
	var $output;
	/**
	 * @var CI_Pagination
	 */
	var $pagination;
	/**
	 * @var CI_Parser
	 */
	var $parser;
	/**
	 * @var CI_Session
	 */
	var $session;
	/**
	 * @var CI_Sha1
	 */
	var $sha1;
	/**
	 * @var CI_Table
	 */
	var $table;
	/**
	 * @var CI_Trackback
	 */
	var $trackback;
	/**
	 * @var CI_Unit_test
	 */
	var $unit;
	/**
	 * @var CI_Upload
	 */
	var $upload;
	/**
	 * @var CI_URI
	 */
	var $uri;
	/**
	 * @var CI_User_agent
	 */
	var $agent;
	/**
	 * @var CI_Validation
	 */
	var $validation;
	/**
	 * @var CI_Xmlrpc
	 */
	var $xmlrpc;
	/**
	 * @var CI_Zip
	 */
	var $zip;
	
	/** CI代码提示 end */
	
	public function __construct()
	{
		parent::__construct();
		$this->init();
	}
	/**
	 * 初始化
	 */
	private function init()
	{
		//载入核心model,用于和数据库交互
		$this->load->model('cic_model');
		$this->load->helper('url');
	}
	
	/**
	 * 获取配置项
	 * @param $config 			'ciccms/site'
	 * @param $item				'web_title'
	 * @param $child_item		'web_title'数组下的元素名称
	 */
	protected  function get_config($config,$item='',$child_item='')
	{
		$this->load->config($config);
		$config = array_pop(explode('/',$config));
		if($item == ''){
			$config = $this->config->item($config);
		}else{
			$config = $this->config->item($item,$config);
			if($child_item != '')
				$config = $config[$child_item];
		}
		return $config;
	}
	/*
	 * 表单验证
	* @param $array  		config下的验证配置项目
	* @param $delimiters	array('<p>','</p>')
	*/
	protected function validate($type,$delimiters='')
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->form_validation->set_message('required', '%s 不能为空！');
		$this->form_validation->set_message('min_length', '%s 最小长度错误！');
		$this->form_validation->set_message('max_length', '%s 长度过长！');
		$this->form_validation->set_message('integer', '%s 必须为数字！');
		if ($this->form_validation->run($type) == FALSE)
		{
			if($delimiters != '')
				$this->form_validation->set_error_delimiters($delimiters[0],$delimiters[1]);
			return validation_errors();
		}
		return TRUE;
	}
	
	/**
	 * 表单验证回调函数：验证select是否选择
	 */
	public function is_null($value)
	{
		if(empty($value))
		{
			$this->form_validation->set_message('is_null', '请选择 %s！');
			return false;
		}else{
			return true;
		}
	}
	/**
	 * 表单验证回调函数：验证唯一性
	 * @param $param [table_name,$field]
	 */
	public function is_unique($value,$param)
	{
		$param = explode(',', $param);
		$row = $this->cic_model->get_one($param[0],array($param[1]=>$value));
		if(empty($row))
		{
			return true;
		}else{
			$this->form_validation->set_message('is_unique', $value.' 已存在！');
			return false;
		}
	}
	/**
	 * 获取无限分类
	 * @param int	 $model_id
	 * @return string
	 */
	protected function get_category_tree($model_id='')
	{
		if($model_id === '')
			$where = '';
		else
			$where = array('model_id'=>$model_id);
		
		$category = $this->cic_model->get_all('category',$where,array('category_sort','asc','category_id','asc'));
		if($category)
		{
			foreach($category->result() as $key=>$value)
			{
				$rows[$key]['category_id'] = $value->category_id;
				$rows[$key]['category_pid'] = $value->category_pid;
				$rows[$key]['category_name'] = $value->category_name;
			}
			$unique_category = $this->get_unique_category($rows);
			return $this->category_tree($rows).$unique_category;
		}
	}
	/**
	 * 获取无限分类遗漏的没有上级分类的分类
	 */
	function get_unique_category($rows)
	{
		$unique_category = $category_id = $category_pid ='';
		foreach($rows as $value)
		{
			$category_id[] = $value['category_id'];
			if($value['category_pid'] != 0)
			{
				$category_pid[$value['category_id']] = $value;
			}	
		}
		if(! empty($category_pid))
		{
			foreach($category_pid as $key=>$value)
			{
				if(! in_array($value['category_pid'], $category_id))
				{
					$unique_category .= '<option value="'.$value['category_id'].'">- - - - - - -'.$value['category_name'].'</option>';
				}
			}
		}
		return $unique_category;
	}
	/**
	 * 递归生成分类
	 * <option></option>
	 */
	private function category_tree($rows,$category_pid=0,$n=0)
	{
		$space = str_repeat('- - ', $n);
		$n++;
		$str='';
		$rows_length = count($rows);
		for($i=0;$i<$rows_length;$i++)
		{
			if($rows[$i]['category_pid'] == $category_pid)
			{
				$str .= '<option value="'.$rows[$i]["category_id"].'">'.$space.$rows[$i]['category_name'].'</option>';
				$str .=$this->category_tree($rows,$rows[$i]['category_id'],$n);
			}
		}
		return $str;	
	}
	/**
	 * 分页
	 */
	protected function page($base_url,$total_rows,$per_page,$page_query_string=FALSE,$uri_segment=3,$cur_tag_open='<span class="current">',$cur_tag_close='</span>')
	{
		$this->load->library('pagination');
		$config = array(
				'base_url'			=> $base_url,
				'total_rows'		=> $total_rows,
				'per_page'			=> $per_page,
				'uri_segment'		=> $uri_segment,
				'page_query_string'	=> $page_query_string,
				'cur_tag_open'		=> $cur_tag_open,
				'cur_tag_close' 	=> $cur_tag_close,
		);
		$this->pagination->initialize($config);
		return $this->pagination->create_links();
	}
}

/**
 * 后台继承类
 */
class CIC_A_Controller extends CIC_Controller
{
	var $admin_config;					//后台menu配置数组		
	var $per_page			= 12;		//后台默认分页每页显示条数
	function __construct()
	{
		parent::__construct();			
		$this->init();
	}
	/**
	 * 初始化
	 */
	private function init()
	{		
		//载入position类：后台获取当前位置与提示		message类：执行成功失败显示的消息类
		$this->load->library(array('admin/position','admin/message'));
		//获取后台top的菜单与left的子菜单
		$this->admin_config 	= $this->get_config('ciccms/admin');
		//判断是否登录
		$this->check_login();
	}
	/**
	 * 头部
	 */
	public function top()
	{
		$data = array(
				'admin_config'		=> $this->admin_config,
				'admin'				=> $this->session->userdata('admin'),
		);
		$this->load->view('admin/include/top',$data);
	}
	/**
	 * 左边
	 */
	public function left()
	{
		if($this->uri->segment(4) === FALSE)
			$key = '首页';
		else
			$key = base64_decode(urldecode($this->uri->segment(4)));
		$data['out_nav']=$this->admin_config['nav'][$key];
		$this->load->view('admin/include/left',$data);
	}
	
	/**
	 * 表单验证
	 * @param $array  config下的验证配置项目
	 * @param $url	 'admin/admin/index'
	 */
	protected function validate($type,$url)
	{
		$result = parent::validate($type,array('<li>','</li>'));
		if($result !== TRUE)
			$this->message->msg($result,site_url($url),5);
	}
	
	/**
	 * 获取ckeditor 配置项
	 */
	protected function ckeditor_config($id)
	{
		return array(
				'id'		=> $id,
				'path'  	=> 'public/common/js/plug/ckeditor',
				'config'	=> array(
						'width'		=> '900px',
						'height'	=> '200px',
						'filebrowserImageBrowseUrl'	=> site_url('admin/image/box_show'),
				),		
		);
	}
	/**
	 * 判断是否登录
	 */
	protected function check_login()
	{
		$is_login = $this->session->userdata('is_login');
		if(! $is_login)
		{
			redirect('admin/login');
		}
	}
	
	protected function admin_priv()
	{
		if($this->uri->segment(1) == 'admin')
		{
			$class 		= $this->uri->segment(2);
			$function	= $this->uri->segment(3);
			$admin = $this->session->userdata('admin');
			if(empty($admin['group_privilege']['all']))
			{
				if(isset($admin['group_privilege'][$class]))
				{
					if(! in_array($function,$admin['group_privilege'][$class]))
					{
						$this->message->msg('无此权限',site_url('admin/admin/main'));
					}
				}else{
					$this->message->msg('无此权限',site_url('admin/admin/main'));
				}
			}
		}
	}
}

/**
 * 前台继承类
 */
class CIC_S_Controller extends CIC_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->init();
	}
	private function init()
	{
		$this->cache();
	}
	/**
	 * 判断是否开启缓存与缓存时间
	 */
	private function cache()
	{
		if($this->get_config('ciccms/site','web_cache'))
		{
			$this->output->cache($this->get_config('ciccms/site','web_cache_time'));
		}
	}
}
